/* File 1 of 2 for Appendix Table 6 */



#delimit ;
clear all;

local outfile "GWgapr10_pfpool_TLsur_LgXage";
set more off;


di _n "$S_DATE $S_TIME";








********************************************************************************;
* Setting up locals for regs;

local labels `"
	phi_f:_cons "Phi female"
	phi_lt25:_cons "Phi age less than 25 yrs"
	phi_40to54:_cons "Phi sge 40 to 54"
	phi_55p:_cons "Phi age 55 plus"
	phi_pt:_cons "Phi part time (<1 FTE)"
	delta:_cons "Delta (exp on av FTEs or hours)"
	
	lnL:_cons "Labour input (ln)"
	xb_multiplant:_cons "Multiplant"
	xb_WPc_WP_cat_1:_cons "1 working proprietor"
	xb_WPc_WP_cat_2:_cons "2-5 working proprietors"
	xb_WPc_WP_cat_3:_cons "6-10 working proprietors"
	xb_WPc_WP_cat_4:_cons ">10 working proprietors"
	
	xb_s_fthc_25to39:_cons "Share of full time employees aged 25 to 39"
	xb_s_pthc_25to39:_cons "Share of part time employees aged 25 to 39"
	xb_s_fthc_40to54:_cons "Share of full time employees aged 40 to 54"
	xb_s_pthc_40to54:_cons "Share of part time employees aged 40 to 54"
	xb_s_fthc_55p:_cons "Share of full time employees aged 55+"
	xb_s_pthc_55p:_cons "Share of full time employees aged 55+"
	
	xb_s_fthc_ten1:_cons "Share of full time employees in 2nd year at pent"
	xb_s_pthc_ten1:_cons "Share of part time employees in 2nd year at pent"
	xb_s_fthc_ten2:_cons "Share of full time employees in 3rd+ year at pent"
	xb_s_pthc_ten2:_cons "Share of part time employees in 3rd+ year at pent"
	
	xb_sec_govt_prod_ent:_cons "Sector: Government producer enterprise"
	xb_sec_fin_inter:_cons "Sector: Financial intermediary"
	xb_avg_age:_cons "Average employee age (/100)"
	xb_female:_cons "Female"
	"';

	
local order "phi_f_lt25_pf:_cons phi_f_25to39_pf:_cons phi_f_40to54_pf:_cons phi_f_55p_pf:_cons
	phi_lt25_pf:_cons phi_40to54_pf:_cons phi_55p_pf:_cons
	phi_pt_pf:_cons delta_pf:_cons
	
	phi_f_lt25_wb:_cons phi_f_25to39_wb:_cons phi_f_40to54_wb:_cons phi_f_55p_wb:_cons
	phi_lt25_wb:_cons phi_40to54_wb:_cons phi_55p_wb:_cons
	phi_pt_wb:_cons delta_wb:_cons
	
	";
	

* uncomment this line to make the output tidier;
*local indicators `"
	"Year fixed effects (pf) = xb_pf_Yyear_*:_cons"
	"Year fixed effects (wb) = xb_wb_Yyear_*:_cons"
	"Productivity industry fixed effects (pf) = xb_pf_INind*:_cons"
	"Productivity industry fixed effects (wb) = xb_wb_INind*:_cons"	
	"'; 

	
	


******************************;
* Specifying the labour input;	

* head count*(average fte)^delta;

local L_fte_del_pf "
	({phi_lt25_pf=0.6} *(L_hc_m_lt25  *(av_fte_m_lt25)^{delta_pf=1} + {phi_f_lt25_pf=0.5}  *L_hc_f_lt25  *(av_fte_f_lt25)^{delta_pf})   
	+ 				 (L_hc_m_25to39*(av_fte_m_25to39)^{delta_pf} + {phi_f_25to39_pf=0.9}*L_hc_f_25to39*(av_fte_f_25to39)^{delta_pf}) 
	+ {phi_40to54_pf=1}*(L_hc_m_40to54*(av_fte_m_40to54)^{delta_pf} + {phi_f_40to54_pf=0.9}*L_hc_f_40to54*(av_fte_f_40to54)^{delta_pf}) 
	+ {phi_55p_pf=0.8} *(L_hc_m_55p   *(av_fte_m_55p)^{delta_pf}    + {phi_f_55p_pf=0.7}   *L_hc_f_55p   *(av_fte_f_55p)^{delta_pf}))";

local L_fte_del_wb "
	({phi_lt25_wb=0.6} *(L_hc_m_lt25  *(av_fte_m_lt25)^{delta_wb=1} + {phi_f_lt25_wb=0.5}  *L_hc_f_lt25  *(av_fte_f_lt25)^{delta_wb})   
	+ 				 (L_hc_m_25to39*(av_fte_m_25to39)^{delta_wb} + {phi_f_25to39_wb=0.9}*L_hc_f_25to39*(av_fte_f_25to39)^{delta_wb}) 
	+ {phi_40to54_wb=1}*(L_hc_m_40to54*(av_fte_m_40to54)^{delta_wb} + {phi_f_40to54_wb=0.9}*L_hc_f_40to54*(av_fte_f_40to54)^{delta_wb}) 
	+ {phi_55p_wb=0.8} *(L_hc_m_55p   *(av_fte_m_55p)^{delta_wb}    + {phi_f_55p_wb=0.7}   *L_hc_f_55p   *(av_fte_f_55p)^{delta_wb}))";

local vars_fte_del "`vars_fte'";

local L_fte_dell "Sum of head count*(average FTEs)^{delta} for all labour types";





	
	
	
	
********************************************************************************;
* Running regressions;

* using obs with hc of 5+ in consisten restricted sample;

use if Q_rest==1 using GWgap_pr_firm_v4, clear;

capture drop *ten*;
capture drop INpf_*;
gen con = 1;
label var con "Constant";


if "`samp'"=="on" {;
	replace pf_ind_gp = 10*floor(pf_ind_gp/10); * making fewer indy FE for sample run;
	replace Yyear_2002 =  Yyear_2002 + Yyear_2003 + Yyear_2004;
	drop Yyear_2003 Yyear_2004;
	replace Yyear_2005 = Yyear_2005 + Yyear_2006 + Yyear_2007;
	drop Yyear_2006 Yyear_2007;
	replace Yyear_2008 = Yyear_2008 + Yyear_2009 + Yyear_2010;
	drop Yyear_2009 Yyear_2010;
};

rename pf_ind_gp ind_gp_pf;
xi i.ind_gp_pf, pre(IN);
 
	
unab extra_ctls: multiplant WPc_* Yyear* INind*;

compress;

capture erase "`outfile'.txt";
file open out using "`outfile'.txt", write;
file write out "The pf and wb regressions are translog form where an observation is
a pent in a year and the labour input is a linear combination of male and female labour
inputs interacted with age groups." _n;

file write out "Regressions are weighted by total FTEs (other)." _n _n;
file close out;





foreach lab in fte_del {; * looping over way labour input is measured;


	eststo clear;
	local outlist `""';

	if "`lab'"=="hc" local wgtvar L_hc_t;
	if "`lab'"=="fte" | "`lab'"=="ftpt" | "`lab'"=="fte_del" | "`lab'"=="hrs" | "`lab'"=="hrs_del"
		local wgtvar L_fte_t;


	local outlist `" `outlist' `lab' "';

	
	di as input _n _n "$S_DATE $S_TIME: Running reg for lab=`lab'";
						
				
	* which labour measure to use;
	
	file open out using "`outfile'.txt", write append;
	di as input "L_pf = `L_`lab'_pf'" _n "`L_`lab'l'";
	file write out "Labour specification: `L_`lab'l'"; 
	file close out;
			
	preserve;
	
	
	* keeping only the subset of controls that vary;
	local extra_ctls2 "";
	foreach var in `extra_ctls' {;
		qui sum `var';
		if r(sd)>0 & r(sd)<. local extra_ctls2 "`extra_ctls2' `var'";
	};
	
	* listing industry FE in extra_ctls2;
	
	local indvars "";
	local nonindvars "";
	foreach var in `extra_ctls2' {;
		if strpos("`var'","INind")==1 local indvars "`indvars' `var'";
		if strpos("`var'","INind")!=1 local nonindvars "`nonindvars' `var'";
	};
	
	* omitting the first industry FE;
	
	local ind1: word 1 of `indvars';
	local indvars: list indvars - ind1;
	local extra_ctls2: list nonindvars | indvars;
		
	
	di "$S_DATE $S_TIME: running pf only to get initial values";
	
	nl (lngo = {lnL_pf=1}*ln(`L_`lab'_pf')	
			+ {lnLsq_pf=1}*ln(`L_`lab'_pf')*ln(`L_`lab'_pf')/100
			+ {lnL_lnK_pf=1}*ln(`L_`lab'_pf')*lnK/100
			+ {lnL_lnM_pf=1}*ln(`L_`lab'_pf')*lnM/100
			
			+ {lnK_pf=1}*lnK
			+ {lnKsq_pf=1}*lnKsq
			+ {lnK_lnM_pf=1}*lnK_lnM
			
			+ {lnM_pf=1}*lnM
			+ {lnMsq_pf=1}*lnMsq
			+ {xb_pf: `extra_ctls2' con})
		[aweight = `wgtvar'];
		
	* saving coeffs to use as starting values;
	matrix define coeffs_pf = e(b);
	
	
	
	di "$S_DATE $S_TIME: running wb only to get starting values";
	
	nl (lnWB = {lnL_wb=1}*ln(`L_`lab'_wb')	
			+ {lnLsq_wb=1}*ln(`L_`lab'_wb')*ln(`L_`lab'_wb')/100
			+ {lnL_lnK_wb=1}*ln(`L_`lab'_wb')*lnK/100
			+ {lnL_lnM_wb=1}*ln(`L_`lab'_wb')*lnM/100
			
			+ {lnK_wb=1}*lnK
			+ {lnKsq_wb=1}*lnKsq
			+ {lnK_lnM_wb=1}*lnK_lnM
			
			+ {lnM_wb=1}*lnM
			+ {lnMsq_wb=1}*lnMsq
			+ {xb_wb: `extra_ctls2' con})
		[aweight = `wgtvar'];
	
	* saving coeffs to use as starting values;
	matrix define coeffs_wb = e(b);
	
	matrix define coeffs = coeffs_pf, coeffs_wb;
	

	di "$S_DATE $S_TIME: running main reg";
	
	nlsur (lngo = {lnL_pf}*ln(`L_`lab'_pf')	
			+ {lnLsq_pf}*ln(`L_`lab'_pf')*ln(`L_`lab'_pf')/100
			+ {lnL_lnK_pf}*ln(`L_`lab'_pf')*lnK/100
			+ {lnL_lnM_pf}*ln(`L_`lab'_pf')*lnM/100
			
			+ {lnK_pf}*lnK
			+ {lnKsq_pf}*lnKsq
			+ {lnK_lnM_pf}*lnK_lnM
			
			+ {lnM_pf}*lnM
			+ {lnMsq_pf}*lnMsq
			+ {xb_pf: `extra_ctls2' con})
		(lnWB = {lnL_wb}*ln(`L_`lab'_wb')	
			+ {lnLsq_wb}*ln(`L_`lab'_wb')*ln(`L_`lab'_wb')/100
			+ {lnL_lnK_wb}*ln(`L_`lab'_wb')*lnK/100
			+ {lnL_lnM_wb}*ln(`L_`lab'_wb')*lnM/100
			
			+ {lnK_wb}*lnK
			+ {lnKsq_wb}*lnKsq
			+ {lnK_lnM_wb}*lnK_lnM
			
			+ {lnM_wb}*lnM
			+ {lnMsq_wb=1}*lnMsq
			+ {xb_wb: `extra_ctls2' con})
		[aweight = `wgtvar']
		, cluster(pent) ifgnls initial(coeffs);
		
	di "Years included in main reg:";
	tab year if e(sample);

		
	di as input "Command just run: " e(cmdline);

	local command = e(cmdline);
	file open out using "`outfile'.txt", write append;
	file write out _n "Command `lab':" _n "`command'" _n _n;
	file close out;
	
	/* use next line to conf observation count;
	rndobs N;
	*/
	

	* Saving the difference between phi in pf and wb equations;
	
	local addsc "";
	local addst "";
	local addn "";

	foreach type in f_lt25 f_25to39 f_40to54 f_55p lt25 40to54 55p {;
	
		local phi_`type'_diff_c = 1 - _b[/phi_`type'_wb]/_b[/phi_`type'_pf];
		
		nlcom 1 - _b[/phi_`type'_wb]/_b[/phi_`type'_pf];
		matrix V = r(V);
		local phi_`type'_diff_se = V[1,1]^0.5;
		
		local addsc "`addsc' phi_`type'_diff_c `phi_`type'_diff_c' phi_`type'_diff_se `phi_`type'_diff_se'";
		local addst "`addst' phi_`type'_diff_c phi_`type'_diff_se";
		local addn `"`addn' "1 - phi_`type'_wb/phi_`type'_pf" "1 - phi_`type'_wb/phi_`type'_pf se" "';
	};
	

	eststo `lab', addscalars(`addsc');
	
			
	restore;



	local addstats "`addst'";
	local addfmts "%9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f %9.3f";
	local addnames `" `addn' "';

	estout * using "`outfile'.txt", append
		c(b(star fmt(%9.3f)) se(par))
		style(tab)
		legend label collabels(, none)
		dropped ("dropped")
		eqlabels(`eqlab', merge) 
		varl(`labels')
		order(`order')
		indicate(`indicators')
		mlabels(`outlist')
		stats(r2 N converge `addstats', fmt(%9.3f %9.0gc %3.0f `addfmts') 
			labels("R-squared" Observations Converged `addnames'))
		varwidth(50) modelwidth(10) delimiter("") wrap
		prehead("`=char(13)'`=char(13)'
			`=char(13)'Run $S_TIME $S_DATE 
			`=char(13)'******************************************************"); 
};


